package com.h3c.nem.service;

import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class ThreadPoolService {

    private ExecutorService pool = null;

    public ThreadPoolService() {
        synchronized (this){
            if(pool == null){
                pool = Executors.newFixedThreadPool(20);
            }
        }
    }

    public void submitTask(Runnable task){
        pool.execute(task);
    }

    @PreDestroy
    public void destroy(){
        if(pool!=null && !pool.isTerminated()){
            pool.shutdownNow();
        }
    }
}
